﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 用面向对象的思想实现双色球
{
    internal class Lotteries : Lottery
    {
        public List<Lottery> lotteries = new List<Lottery>();
        public List<Ball> LuckyBall = new List<Ball>();
        bool isDraw = false;
        bool isBuy = false;
        public Lotteries() { }

        public void PrintMenu()
        {
            //展示菜单
            Console.WriteLine("--------------------------");
            Console.WriteLine("  欢迎使用控制台版双色球");
            Console.WriteLine("--------------------------");
        }

        public void Start()
        {
            while (true)
            {
                Console.WriteLine("1. 购买彩票");
                Console.WriteLine("2. 查看已购彩票");
                Console.WriteLine("3. 开奖");
                Console.WriteLine("4. 查看中奖情况");
                Console.WriteLine("5. 退出系统");
                Console.WriteLine();
                //接收用户输入的编号
                string num = Console.ReadLine();
                switch (num)
                {
                    case "1":
                        Console.WriteLine("购买彩票");
                        DisposeBall();
                        break;
                    case "2":
                        PrintLottery();
                        break;
                    case "3":
                        LotteryDraw();
                        break;
                    case "4":
                        CheckWinnings();
                        break;
                    case "5":
                        Console.WriteLine("感谢您的使用，再见");
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("你的输入有误，请重新输入");
                        Console.ResetColor();
                        break;

                }
                break;
            }

        }
        public class BallComparer : IComparer<Ball>
        {
            public int Compare(Ball x, Ball y)
            {
                return x.num.CompareTo(y.num);
            }
        }
        public void DisposeBall()
        {
            Lottery lottery = new Lottery();
            while (true)
            {
                Console.WriteLine($"请输入你要选择的第{lottery.redBall.Count + 1}个红球");
                int redNum = Convert.ToInt32(Console.ReadLine());
                //判断输入红球号码所属范围是否正确
                if (redNum < 1 || redNum > 33)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n输入的红球超出了范围(1-33)");
                    Console.ResetColor();
                    continue;
                }
                //判断是否输入重复的红球
                bool isRedNumRepeat = false;
                for (int i = 0; i < lottery.redBall.Count; i++)
                {
                    if (lottery.redBall[i].num == redNum)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("\n购买的红球重复!");
                        Console.ResetColor();
                        isRedNumRepeat = true;
                        break;
                    }
                }
                if (isRedNumRepeat)
                {
                    isRedNumRepeat = false;
                    continue;
                }
                //将当前判断无误的红球放入到当前的彩票数组中
                lottery.redBall.Add(new Ball(redNum));

                if (lottery.redBall.Count == 6)
                {
                    lottery.redBall.Sort(new BallComparer());
                    break;
                }

            }
            while (true)
            {
                Console.WriteLine($"请输入你要选择的蓝球");
                int blueNum = Convert.ToInt32(Console.ReadLine());
                //判断输入蓝球号码所属范围是否正确
                if (blueNum < 1 || blueNum > 16)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n输入的蓝球超出了范围(1-16)");
                    Console.ResetColor();
                    continue;
                }

                //将当前判断无误的蓝球放入到当前的彩票数组中
                lottery.blueBall.num = blueNum;
                break;
            }
            while (true)
            {
                Console.WriteLine($"请输入你要选择的倍数");
                int multiple = Convert.ToInt32(Console.ReadLine());
                //判断输入倍数所属范围是否正确
                if (multiple < 1 || multiple > 100)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n输入的蓝球超出了范围(1-100)");
                    Console.ResetColor();
                    continue;
                }

                //将当前判断无误的倍数放入到当前的彩票数组中
                lottery.multiple = multiple;
                break;
            }
            lotteries.Add(lottery);
            isBuy = true;
            Console.WriteLine();
        }
        public void PrintLottery()
        {
            foreach (var lottery in lotteries)
            {
                Console.WriteLine("已购买彩票: ");
                foreach (var ball in lottery.redBall)
                {
                    Console.Write(ball.num + " ");
                }
                Console.Write("- " + lottery.blueBall.num);
                Console.WriteLine(" * " + lottery.multiple);
            }
        }
        public void LotteryDraw()
        {
            if (isBuy)
            {
                if(isDraw)
                {
                    Console.WriteLine("已经开奖了");
                }
                else
                {
                    Console.WriteLine("开奖");
                    Random rnd = new Random();
                    while (true)
                    {
                        int redNum = rnd.Next(1, 34);
                        bool redBallRepeat = false;
                        for (int i = 0; i < LuckyBall.Count; i++)
                        {
                            if (LuckyBall[i].num == redNum)
                            {
                                redBallRepeat = true;
                                break;
                            }
                        }
                        if (redBallRepeat)//随机红球号码出现重复
                        {
                            continue;
                        }
                        LuckyBall.Add(new Ball(redNum));

                        if (LuckyBall.Count == 6)
                        {
                            break;
                        }
                    }
                    //对开奖的红球进行排序
                    LuckyBall.Sort(new BallComparer());
                    //处理开奖的蓝球
                    LuckyBall.Add(new Ball(rnd.Next(1, 17)));
                    isDraw = true;
                    foreach(Ball b in LuckyBall)
                    {
                        Console.Write(b.num + " ");
                    }
                    Console.WriteLine();

                }

            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("请先购买彩票! ");
                Console.ResetColor();
            }
        }

        public void CheckWinnings()
        {
            if (isDraw)
            {
                int redWinCount = 0;
                int blueWinCount = 0;
                //判断中奖红球号码
                for (int i = 0; i < lotteries.Count; i++)
                {
                    for (int j = 0; j < lotteries[i].redBall.Count; j++)
                    {
                        for (int k = 0; k < 6; k++)
                        {
                            if (LuckyBall[k].num == lotteries[i].redBall[j].num)
                            {
                                redWinCount++;
                            }
                        }
                    }
                    //判断中奖蓝球
                    if (lotteries[i].blueBall.num == LuckyBall[6].num)
                    {
                        blueWinCount++;
                    }
                    //中奖注
                    #region
                    Console.WriteLine($"第{i + 1}注:");
                    if (redWinCount == 6 && blueWinCount == 1)
                    {
                        Console.WriteLine($"恭喜您中的一等奖, 中奖金额为: 500万元 * {+lotteries[i].multiple}");
                    }
                    else if (redWinCount == 6 && blueWinCount == 0)
                    {
                        Console.WriteLine($"恭喜您中的二等奖, 中奖金额为: 5万元 * {+lotteries[i].multiple}倍");
                    }
                    else if (redWinCount == 5 && blueWinCount == 1)
                    {
                        Console.WriteLine($"恭喜您中的三等奖, 中奖金额为: 3000元 * {+lotteries[i].multiple}倍");
                    }
                    else if ((redWinCount == 5 && blueWinCount == 0)
                        || (redWinCount == 4 && blueWinCount == 1))
                    {
                        Console.WriteLine($"恭喜您中的四等奖, 中奖金额为: 200元 * {+lotteries[i].multiple}倍");
                    }
                    else if ((redWinCount == 4 && blueWinCount == 0)
                        || (redWinCount == 3 && blueWinCount == 1))
                    {
                        Console.WriteLine($"恭喜您中的五等奖, 中奖金额为: 10元 * {+lotteries[i].multiple}倍");
                    }
                    else if ((redWinCount == 2 && blueWinCount == 1)
                        || (redWinCount == 1 && blueWinCount == 1)
                        || (redWinCount == 0 && blueWinCount == 1))
                    {
                        Console.WriteLine($"恭喜您中的六等奖, 中奖金额为: 5元 * {+lotteries[i].multiple}倍");
                    }
                    else
                    {
                        Console.WriteLine("谢谢惠顾");
                    }
                    #endregion
                }
            }
            else
            {
                Console.WriteLine("\n请先开奖");
            }
            Console.WriteLine();
        }
    }

}

